/*
 / _____)             _              | |
( (____  _____ ____ _| |_ _____  ____| |__
 \____ \| ___ |    (_   _) ___ |/ ___)  _ \
 _____) ) ____| | | || |_| ____( (___| | | |
(______/|_____)_|_|_| \__)_____)\____)_| |_|
    (C)2013 Semtech

Description: SX1276 driver specific target board functions implementation

License: Revised BSD License, see LICENSE.TXT file include in the project

Maintainer: Miguel Luis and Gregory Cristian
*/

#include "sx127x-board.h"
#include "radio.h"
#include "sx127x_dio.h"
#include "sx127x_spi.h"
#include "sx127x_timer.h"
#include <stdio.h>

/*!
 * Flag used to set the RF switch control pins in low power mode when the radio is not active.
 */
static bool RadioIsActive = false;

/*!
 * Radio driver structure initialization
 */
const struct Radio_s Radio =
{
    SX1276Init,
    SX1276GetStatus,
    SX1276SetModem,
    SX1276SetChannel,
    SX1276IsChannelFree,
    SX1276Random,
    SX1276SetRxConfig,
    SX1276SetTxConfig,
    SX1276CheckRfFrequency,
    SX1276GetTimeOnAir,
    SX1276Send,
    SX1276SetSleep,
    SX1276SetStby, 
    SX1276SetRx,
    SX1276StartCad,
    SX1276ReadRssi,
    SX1276Write,
    SX1276Read,
    SX1276WriteBuffer,
    SX1276ReadBuffer,
    SX1276SetMaxPayloadLength
};


void SX1276InitStruct( SX1276_t *a_ptSx1276 )
{
    a_ptSx1276->hReset = sx127x_DioReset;
    a_ptSx1276->hWrite = SX127xWrite;
    a_ptSx1276->hRead = SX127xRead;
    a_ptSx1276->hWriteBuffer = SX127xWriteBuffer;
    a_ptSx1276->hReadBuffer = SX127xReadBuffer;
    a_ptSx1276->HfLf = LF_FREQ;
}


extern DioIrqHandler *DioIrq[];
TimerEvent_t Dio0TimerEvt;
TimerEvent_t Dio1TimerEvt;
TimerEvent_t Dio2TimerEvt;
TimerEvent_t Dio3TimerEvt;
TimerEvent_t Dio4TimerEvt;
static bool bInitTimer = false;
/*!
 * Antenna switch GPIO pins objects
 */

void SX1276BoardInit( void )
{
    sx127x_TimerInit();
    if ( false == bInitTimer) {
        bInitTimer = true;         
    }
    sx127x_DioInit();
    sx127x_SpiInit();
    SX1276InitStruct( &SX1276 );
}

uint8_t SX1276GetPaSelect( uint32_t channel )
{
    if( channel < RF_MID_BAND_THRESH )
    {
        return RF_PACONFIG_PASELECT_PABOOST;
    }
    else
    {
        return RF_PACONFIG_PASELECT_RFO;
    }
}

void SX1276SetAntSwLowPower( bool status )
{
    if( RadioIsActive != status )
    {
        RadioIsActive = status;
    
        if( status == false )
        {
            SX1276AntSwInit( );
        }
        else
        {
            SX1276AntSwDeInit( );
        }
    }
}

void SX1276AntSwInit( void )
{

}

void SX1276AntSwDeInit( void )
{


}

void SX1276SetAntSw( uint8_t rxTx )
{
    SX1276.RxTx = rxTx;
    
    if(SX1276.HfLf == LF_FREQ){ // LF
        printf("**Low Freq - ");
        sx127x_Gpio5Set(true);
        if( rxTx == 0 ){ // 1: TX, 0: RX
            printf("RX .\n");
            sx127x_Gpio4Set(true);
        }else{
            printf("TX .\n");
            sx127x_Gpio4Set(false);
        }
    }else{  //HF
        printf("**High Freq - ");
        sx127x_Gpio5Set(false);
        if( rxTx == 0 ){ // 1: TX, 0: RX
            printf("RX .\n");
            sx127x_Gpio6Set(false);
        }else{
            printf("TX .\n");
            sx127x_Gpio6Set(true);
        } 
    }
#if 0    
    if( rxTx == 0 ){ // 1: TX, 0: RX
        if(SX1276.HfLf == LF_FREQ) // 1:Hf, 0 :Lf
        {
            sx127x_Gpio5Set(true);
            sx127x_Gpio4Set(true);
            printf("**Low Freq RX.\n");
        }else{
            sx127x_Gpio5Set(false);
            sx127x_Gpio6Set(true);
            printf("**High Freq RX.\n");
        }
    }
    else //tx
    {
        if(SX1276.HfLf == LF_FREQ) // 1:Hf, 0 :Lf
        {
            sx127x_Gpio5Set(true);
            sx127x_Gpio4Set(false);
            printf("**Low Freq TX.\n");
        }else{
            sx127x_Gpio5Set(false);
            sx127x_Gpio6Set(false);
            printf("**High Freq TX.\n");
        }        
    }
#endif    
}


uint8_t SX1276CheckRfFrequency( uint32_t frequency )
{
    if( frequency < RF_MID_BAND_THRESH )
    {
        SX1276.HfLf = LF_FREQ;
        return LF_FREQ;
    }
    else
    {
        SX1276.HfLf = HF_FREQ;
        return HF_FREQ;
    }
}


void sx1278Dio0_Isr( void )
{
    //TimerStart( &Dio0TimerEvt );
    DioIrq[0]();
}

void sx1278Dio1_Isr( void )
{
    //TimerStart( &Dio1TimerEvt );
    DioIrq[1]();
}

void sx1278Dio2_Isr( void )
{
    //TimerStart( &Dio2TimerEvt );
    DioIrq[2]();
}
void sx1278Dio3_Isr( void )
{
    //TimerStart( &Dio3TimerEvt );
    DioIrq[3]();
}
void sx1278Dio4_Isr( void )
{
    //TimerStart( &Dio4TimerEvt );
    DioIrq[4]();
}

void sx1276_StopDioTimerEvent( void )
{
    ;/* clear dio it flag */

    TimerStop( &Dio0TimerEvt );
    TimerStop( &Dio1TimerEvt );
    TimerStop( &Dio2TimerEvt );
    TimerStop( &Dio3TimerEvt );
    TimerStop( &Dio4TimerEvt );
}
